<?php
include_once("team.php");
if(class_exists('SQL_Helper') != true)
{
   include_once("sql_helper.php");
}
class TeamRepository
{

function createTeam($team_name, $divisionID, $password){
	SQL_Helper::executeNonQuery("INSERT INTO `teams` (`teamID`, `team_name`, `divisionID`, `team_status`, `password`) VALUES(null,?,?,'Active',?)",$team_name, $divisionID, $password);
}

function readTeam($teamID){
	return SQL_Helper::executeQuerySingle("Select * from `teams` where `teamID` = ?", $teamID);
}

function updateTeam($teamID, $team_name, $divisionID, $team_status){
	SQL_Helper::executeNonQuery("Update `teams` set `team_name`=?, `divisionID`=?, `team_status`=? where `teamID` = ?",$team_name, $divisionID, $team_status, $teamID);
}

function updateTeamPassword($teamID, $password){
	SQL_Helper::executeNonQuery("Update `teams` set `password`=? where `teamID` = ?",$password, $teamID);
}

function deleteTeam($teamID){
	SQL_Helper::executeNonQuery("delete from `teams` where `teamID` = ?", $teamID);
}

function map($row){
	$class= "Team";
	return SQL_Helper::sql_map($row,$class);
}

function maps($rows){
	$class= "Team";
	return SQL_Helper::sql_maps($rows,$class);
}

function getPlayerByTeamID($teamID){
	$playerId = array();
	$row=SQL_Helper::executeQuery("Select * from `teammembers` where `teamID` = ?", $teamID);
	foreach ($row  as $r)
	{
		$playerId[]=$r['playerID'];
	}
	
	return $playerId;
}

function isValidLogin ($username, $password){
	$row=SQL_Helper::executeQuery("Select `password` from `teams` where `teamID` = ?", $username);
	return $password==$row;
}

function getPlayers1ByRoundScheduleID($round_scheduleID){
		$player1Id = array();
		$row=SQL_Helper::executeQuery("SELECT distinct `player1ID` FROM `match` WHERE `round_scheduleID`=?", $round_scheduleID);
		foreach ($row  as $r){
		$player1Id[]=$r['player1ID'];
			}
		return $player1Id;
	}
	
function getPlayers2ByRoundScheduleID($round_scheduleID){
		$player2Id = array();
		$row=SQL_Helper::executeQuery("SELECT distinct `player2ID` FROM `match` WHERE `round_scheduleID`=?", $round_scheduleID);
		foreach ($row  as $r){
		$player2Id[]=$r['player2ID'];
			}
		return $player2Id;
	}
	
function getActiveTeams($divisionID){
	return SQL_Helper::executeQuery("SELECT * FROM `teams` WHERE `team_status`='Active' and divisionID=?",$divisionID);
}

function getTeamIDByPlayer($playerID){
		$row= SQL_Helper::executeQuerySingle("SELECT `x1`.`teamID` FROM `teammembers` as `x1` inner join `teams` as `x2` on `x1`.`teamID`=`x2`.`teamID` and `x2`.`team_status`='Active' WHERE `playerID`=?", $playerID);
		 return $row[0];
	}

function deleteTeamMembers($teamID){
	SQL_Helper::executeNonQuery("delete from `teammembers` where `teamID`=?",$teamID);
}


function AssignTeamMembers($teamID,$playerID){
	SQL_Helper::executeNonQuery("insert into `teammembers` (`teamID`,`playerID`) values (?,?)",$teamID, $playerID);
}
}
?>